package design.chain;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yuqi<307530884@qq.com> <2017/3/22>
 * @version 1.0
 * @since 1.0
 */
public class RequestMaker {

    public static void main(String[] args) {
        //初始化责任链
        List<Filter> filters = new ArrayList<Filter>();
        for(int i = 0 ; i < 2 ; i++){
            Filter filter = new FilterImpl(i, "filter" + i);
            filters.add(filter);
        }


        final FilterChainImpl filterChain = new FilterChainImpl();
        filterChain.registerAll(filters);

        Thread[] threads = new Thread[10];
        for(int i = 0 ; i < 10 ; i++){
            final int index = i;
            threads[i] = new Thread(new Runnable() {
                @Override
                public void run() {
                    Request request = new Request();
                    request.setReqCode(index);
                    Response response = filterChain.doFilter(request);
                    System.out.println("处理结果：" + response);
                    System.out.println("-------------------------");
                }
            });
        }

        for(Thread thread : threads){
            thread.start();
        }

        for(Thread thread : threads){
            try {
                thread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        System.out.println("over");

    }
}
